<template>
  <div>
    <el-table :data="resources">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="fileName" label="文件名"></el-table-column>
      <el-table-column prop="filePath" label="文件路径"></el-table-column>
      <el-table-column prop="category.name" label="分类"></el-table-column>
      <el-table-column prop="uploadedAt" label="上传时间" :formatter="formatDate"></el-table-column>
      <el-table-column prop="description" label="描述"></el-table-column>
      <el-table-column prop="uploader.username" label="上传者"></el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import axios from '../plugins/axios'

interface Resource {
  id: number;
  fileName: string;
  filePath: string;
  category: {
    id: number;
    name: string;
    parentId: number | null;
    createdAt: string;
    updatedAt: string;
    postIds: number[];
  };
  uploadedAt: string;
  description: string;
  uploader: {
    id: number;
    username: string;
    password: string;
    email: string;
    fullName: string | null;
    gender: string | null;
    phone: string | null;
    birthDate: string | null;
    undergraduateSchool: string | null;
    targetUniversity: string | null;
    personalBio: string | null;
    role: string;
  };
}

const resources = ref<Resource[]>([])

const fetchResources = async () => {
  try {
    const response = await axios.get('/resources/all')
    resources.value = response.data
  } catch (error) {
    console.error('Failed to fetch resources:', error)
  }
}

// 日期格式化函数
const formatDate = (row: any, column: any, cellValue: string) => {
  const date = new Date(cellValue)
  const options: Intl.DateTimeFormatOptions = {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  }
  return date.toLocaleDateString('zh-CN', options)
}

onMounted(() => {
  fetchResources()
})
</script>

<style scoped>
.el-table {
  width: 100%;
}
</style>